/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ytmc.uploader;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.AbstractListModel;
import ytmc.Model;
import ytmc.setup.*;

/**
 *
 * @author K
 */
public class AddUploadTasksModel extends AbstractListModel implements Model
{
    private ArrayList<UploadTask> tasks;

    public  Collection<UploadTask> getTasksToAdd()
    {
        return tasks;
    }

    public void addTasksFromPath(String path) throws IOException
    {
        File file = new File(path);
        if(file.isDirectory())
        {
            String[] fileNames = file.list(new FilenameFilter()
            {
                @Override
                public boolean accept(File dir, String name)
                {
                    for (String extenstion : VideoFormat.friendlyNames())
                    {
                        if (name.toLowerCase().endsWith("." + extenstion))
                            return true;
                    }

                    return false;
                }
            });

            for(String fileName : fileNames)
            {
                File uploadTaskFile = new File(file.getAbsolutePath() + File.separatorChar + fileName);
                int position = tasks.size();
                tasks.add(new UploadTask(uploadTaskFile.getAbsolutePath()));
                fireContentsChanged(this, position, position);
            }
        }
        else
        {
            int position = tasks.size();
            tasks.add(new UploadTask(path));
            fireContentsChanged(this, position, position);
        }

        if(tasks.size() > 0)
        {
            setSelectedTask(0);
        }
    }

    public void removeSelectedTask()
    {
        if (selectedTask == null)
            return;

        int index = tasks.indexOf(selectedTask);
        tasks.remove(index);

        if (index < 0)
        {
            index = 0;
            selectedTask = tasks.get(index);
        }
        else if (index >= tasks.size())
        {
            index = tasks.size() - 1;
            selectedTask = tasks.get(index);
        }

        fireContentsChanged(this, index, index);
        notifyPropertyChangeListeners(new PropertyChangeEvent(this, "selectedTask", null, selectedTask));
    }

    public void clearTasks()
    {
        tasks.clear();
    }

    public boolean areTasksValid()
    {
        for(UploadTask task : tasks)
        {
            if(task.getTitle().isEmpty())
                return false;
        }

        return true;
    }

    private UploadTask selectedTask;

    public UploadTask getSelectedTask()
    {
        return selectedTask;
    }

    public void setSelectedTask(UploadTask selectedTask)
    {
        if(selectedTask != this.selectedTask)
        {
            UploadTask old = this.selectedTask;
            this.selectedTask = selectedTask;
            notifyPropertyChangeListeners(new PropertyChangeEvent(this, "selectedTask", old, selectedTask));
        }
    }

    public void setSelectedTask(int index)
    {
        UploadTask newSelectedTask = null;

        if(index >= 0 && index < tasks.size())
        {
            newSelectedTask = tasks.get(index);
        }

        if(newSelectedTask != this.selectedTask)
        {
            UploadTask old = this.selectedTask;
            this.selectedTask = newSelectedTask;
            notifyPropertyChangeListeners(new PropertyChangeEvent(this, "selectedTask", old, newSelectedTask));
        }
    }

    private ArrayList<PropertyChangeListener> listeners;

    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        if (!listeners.contains(listener))
        {
            listeners.add(listener);
        }
    }

    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        if (listeners.contains(listener))
        {
            listeners.remove(listener);
        }
    }

    private void notifyPropertyChangeListeners(PropertyChangeEvent event)
    {
        for (PropertyChangeListener propertyChangeListener : listeners)
        {
            propertyChangeListener.propertyChange(event);
        }
    }

    public AddUploadTasksModel()
    {
        this.tasks = new ArrayList<UploadTask>();
        this.listeners = new ArrayList<PropertyChangeListener>();
    }

    @Override
    public int getSize()
    {
        return tasks.size();
    }

    @Override
    public Object getElementAt(int index)
    {
        if(index >= 0 && index < tasks.size())
        {
            return tasks.get(index).getFilePath();
        }

        return null;
    }
}
